package com.caishi.lkx.order.pay;

import com.caishi.lkx.exam.ienum.status.ProductCodeStatus;
import com.caishi.lkx.exam.service.IProductCodeService;
import com.caishi.lkx.order.model.PaymentModel;
import com.caishi.lkx.order.model.PaymentRefundLogModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by keray
 * date:2021/7/15 10:24 上午
 */
@Configuration
@Slf4j
public class CodePay implements PayPlugins {

    @Resource
    private IProductCodeService productCodeService;

    @Override
    public Map<String, Object> sign(PaymentModel paymentModel, Object val, Map<String, Object> otherParam) {
        return new HashMap<>();
    }

    @Override
    public boolean prePayCallBack(PaymentModel paymentModel) {
        var model = productCodeService.getById(paymentModel.getMode());
        return model != null && model.getStatus() == ProductCodeStatus.effective;
    }

    @Override
    public void postPayCallBack(PaymentModel paymentModel) {
        productCodeService.changeStatus(paymentModel.getMode(), ProductCodeStatus.used, paymentModel.getPayUserId());
    }

    @Override
    public boolean refund(PaymentModel paymentModel, PaymentRefundLogModel paymentRefundLogModel) {
        productCodeService.changeStatus(paymentModel.getMode(), ProductCodeStatus.effective, paymentModel.getPayUserId());
        return true;
    }


}
